home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 November: Tool Chest / Dev.CD Nov 98 TC.toast / Sample Code / Networking / OTStreamLogViewer1.0b2 / LogEngine / TestLogEngine.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-07-06  |  4.1 KB  |  170 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        StreamLogWatcher.c
  3.  
  4.     Contains:    A program to display information logged to the STREAMS log module.
  5.  
  6.     Written by:    Quinn "The Eskimo!"
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.     You may incorporate this sample code into your applications without
  13.     restriction, though the sample code has been provided "AS IS" and the
  14.     responsibility for its operation is 100% yours.  However, what you are
  15.     not permitted to do is to redistribute the source as "DSC Sample Code"
  16.     after having made changes. If you're going to re-distribute the source,
  17.     we require that you make it clear in the source that the code was
  18.     descended from Apple Sample Code, but that you've made changes.
  19. */
  20.  
  21. #define qDebug 1
  22.  
  23. /////////////////////////////////////////////////////////////////////
  24. // Pick up the standard C stuff.
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28.  
  29. /////////////////////////////////////////////////////////////////////
  30. // Pick up standard OT APIs.
  31.  
  32. #include <OpenTptInternet.h>
  33.  
  34. /////////////////////////////////////////////////////////////////////
  35. // Pick up low-level OT APIs.
  36.  
  37. #include <OpenTptClient.h>
  38. #include <OTDebug.h>
  39. #include <strlog.h>
  40. #include <stropts.h>
  41. #include <mistream.h>
  42.  
  43. /////////////////////////////////////////////////////////////////////
  44. // Pick up the symbolic name of the various OT modules.
  45.  
  46. #include <modnames.h>
  47.  
  48. /////////////////////////////////////////////////////////////////////
  49. // Pick up the log engine prototypes.
  50.  
  51. #include "LogEngine.h"
  52.  
  53. /////////////////////////////////////////////////////////////////////
  54. // OTDebugStr is not defined in any OT header files, but it is
  55. // exported by the libraries, so we define the prototype here.
  56.  
  57. extern pascal void OTDebugStr(const char* str);
  58.  
  59. /////////////////////////////////////////////////////////////////////
  60.  
  61. /////////////////////////////////////////////////////////////////////
  62.  
  63. static pascal void PrintLogEntry(LogEntryPtr thisEntry, void *refCon)
  64. {
  65.     #pragma unused(refCon)
  66.     #pragma unused(thisEntry)
  67.     
  68.     printf("••• got message “%s”\n", ((char *) thisEntry) + sizeof(LogEntry));
  69. }
  70.  
  71. /////////////////////////////////////////////////////////////////////
  72.  
  73. static void PrintHelp(void)
  74. {
  75.     printf("s) start/stop logging\n");
  76.     printf("l) log something\n");
  77.     printf("L) log 10 somethings\n");
  78.     printf("i) idle the kernel (don't ask why you have to do this!)\n");
  79.     printf("p) print new log entries\n");
  80.     printf("q) quit\n");
  81.     printf("?) print help\n");
  82.     printf("\n");
  83. }
  84.  
  85. extern void main(void)
  86. {
  87.     OSStatus err;
  88.     Boolean quitNow;
  89.     char command[256];
  90.     int i;
  91.     
  92.     printf("Hello Cruel World\n");
  93.     
  94.     err = InitOpenTransport();
  95.     if (err == noErr) {
  96.     
  97.         PrintHelp();
  98.         quitNow = false;
  99.         do {
  100.             printf("Enter command:\n");
  101.             gets(command);
  102.             switch (command[0]) {
  103.                 case '?':
  104.                     PrintHelp();
  105.                     break;
  106.                 case 's':
  107.                     if ( ! LoggingActive() ) {
  108.                         printf("StartLogging\n");
  109.                         fflush(stdout);
  110.                         err = StartLogging(true, 0, nil);
  111.                         if (err != noErr) {
  112.                             printf("Error starting logging %ld\n", err);
  113.                             err = noErr;
  114.                         }
  115.                     } else {
  116.                         printf("StopLogging\n");
  117.                         fflush(stdout);
  118.                         StopLogging();
  119.                     }
  120.                     break;
  121.                 case 'i':
  122.                     OTIdle();
  123.                     break;
  124.                 case 'p':
  125.                     ForEachNewLogEntry(PrintLogEntry, nil);
  126.                     break;
  127.                 case 'q':
  128.                     quitNow = true;
  129.                     break;
  130.                 case 'l':
  131.                     printf("Calling strlog\n");
  132.                     strlog(1, 2, 3, SL_TRACE | SL_ERROR, "Hello Cruel World!");
  133.                     break;
  134.                 case 'L':
  135.                     for (i = 0; i < 10; i++) {
  136.                         printf("Calling strlog\n");
  137.                         strlog(1, 2, 3, SL_TRACE | SL_ERROR, "Hello Cruel World!");
  138.                     }
  139.                     break;
  140.                 case '!':
  141.                     for (i = 0; i < 32000; i++) {
  142.                         // OTIdle();
  143.                         strlog(1, 2, 3, SL_TRACE | SL_ERROR, "Hello Cruel World!");
  144.                         if ( (i % 100) == 0) {
  145.                             printf(".");
  146.                             fflush(stdout);
  147.                         }
  148.                     }
  149.                     break;
  150.                 default:
  151.                     printf("Huh?\n");
  152.                     break;
  153.             }
  154.         } while ( ! quitNow );
  155.     
  156.         if ( LoggingActive() ) {
  157.             printf("StopLogging\n");
  158.             fflush(stdout);
  159.             StopLogging();
  160.         }
  161.     
  162.         CloseOpenTransport();
  163.     }
  164.     
  165.     if (err == noErr) {
  166.         printf("Success!\n");
  167.     } else {
  168.         printf("Failed with error %ld.\n", err);
  169.     }
  170. }